using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Metro.Admin.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;   

namespace Metro.Admin.DataAccess
{
    partial class AdminDataSource
    {
        Database metroDb = DatabaseFactory.CreateDatabase("metroDB");


        private List<Parameter> parameters=null;


        public ICollection<Parameter> GetParameters(string type)
        {
            string name = string.Empty;
            string value = string.Empty;
            string typeParameter = string.Empty;
 
            try
            {
                parameters = new List<Parameter>();
                string sqlCommand = "Select [name],[value],[type] From Parameter where type = @type";
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                metroDb.AddInParameter(dbCommand, "@type", DbType.String, type);
                using (IDataReader dataReader = metroDb.ExecuteReader(dbCommand))
                {
                    while (dataReader.Read())
                    {
                        // Get the value of the Name column in the DbDataReader.
                       name = dataReader["name"].ToString() ;
                       value = dataReader["value"].ToString();
                       typeParameter = dataReader["type"].ToString();

                       parameters.Add(new Parameter(name, value, typeParameter));
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;            
            }

            return parameters;
        }
        public void UpdateParameter(Parameter parameter)
        {
            try
            {
                string sqlCommand = "UPDATE [Parameter] SET [value] = @value, [date]=@date WHERE [name] = @name";
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                metroDb.AddInParameter(dbCommand, "@name", DbType.String, parameter.Name);
                metroDb.AddInParameter(dbCommand, "@value", DbType.String, parameter.Value);
                metroDb.AddInParameter(dbCommand, "@date", DbType.DateTime, DateTime.Now);
                metroDb.ExecuteNonQuery(dbCommand);  
            
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
        public void CreateParameter(Parameter parameter)
        {
            try
            {
                string sqlCommand = "INSERT INTO [Parameter] ([name],[value],[type],[date]) VALUES  (@name,@value,'O',@date)";
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                metroDb.AddInParameter(dbCommand, "@name", DbType.String, parameter.Name);
                metroDb.AddInParameter(dbCommand, "@value", DbType.String, parameter.Value);
                metroDb.AddInParameter(dbCommand, "@date", DbType.DateTime, DateTime.Now);
                metroDb.ExecuteNonQuery(dbCommand);  
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void DeleteParameter(Parameter parameter)
        {
            try
            {
                string sqlCommand = "DELETE FROM [Parameter] WHERE [name] = @name";
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                metroDb.AddInParameter(dbCommand, "@name", DbType.String, parameter.Name);
                metroDb.ExecuteNonQuery(dbCommand);  
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        
    }
}
